package com.ljfresh.customer.job;

import com.alibaba.fastjson.JSONObject;
import com.ljfresh.customer.erp.ERPBasicAPIUtil;
import com.ljfresh.customer.erp.ERPConsts;
import com.ljfresh.customer.pojo.AbnormalGoods;
import com.ljfresh.customer.pojo.ErpGoods;
import com.ljfresh.customer.result.GoodERPResult;
import com.ljfresh.customer.result.TokenResult;
import com.ljfresh.customer.sercice.AbnormalGoodService;
import com.ljfresh.customer.sercice.ErpGoodsService;
import com.ljfresh.customer.sercice.TwiterIdService;
import com.ljfresh.customer.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: Lucifer
 * @time: 2020/4/14-10:21
 */
@Component
public class GoodsJob {

    @Resource
    private RedisUtils redisUtils;

    @Autowired
    private TwiterIdService twiterIdService;

    @Resource
    private ErpGoodsService erpGoodsService;

    //@Scheduled(cron = "20 36 14 * * ?")  //每天一点二十分
    public void GoodSysnJob(){
        TokenResult tokenResult = JSONObject.parseObject(redisUtils.get(ERPConsts.erpTokenRedisKey).toString(), TokenResult.class);
        if (tokenResult!=null){
            erpGoodsService.delErpGoods();
            for (int i = 1;i <= 100; i++){
                List<GoodERPResult> goodERPResults = ERPBasicAPIUtil.findGoods(tokenResult.getRefresh_token(),i);
                if (goodERPResults.size()>0){
                    for (GoodERPResult goodERPResult : goodERPResults) {
                        ErpGoods erpGoods = new ErpGoods();
                        erpGoods.setId(twiterIdService.getTwiterId());
                        erpGoods.setGoodsId(Integer.parseInt(goodERPResult.getItem_code()));
                        erpGoods.setGoodsName(goodERPResult.getItem_name());
                        erpGoodsService.save(erpGoods);
                    }
                }
                if (goodERPResults.size()<500){
                    return;
                }
            }
        }
    }
}
